﻿using System;
using Company.BLL;
using Company.BLL.DuLieuChuan;
using Company.BLL.KDT.SXXK;
using Janus.Windows.GridEX;
using Janus.Windows.UI.CommandBars;

namespace Company.Interface.KDT.SXXK
{
    public partial class SanPhamDangKyDetailForm : BaseForm
    {
        public SanPhamDangKy SPDangKy = new SanPhamDangKy();

        //-----------------------------------------------------------------------------------------
        public SanPhamDangKyDetailForm()
        {
            InitializeComponent();
        }

        #region Private methods.

        //-----------------------------------------------------------------------------------------
        private void SanPhamDangKyDetailForm_Load(object sender, EventArgs e)
        {
            this.SPDangKy.LoadSPCollection();
            dgList.DataSource = this.SPDangKy.SPCollection;
            switch (this.SPDangKy.TrangThaiXuLy)
            {
                case -1:
                    lblTrangThai.Text = "Chưa gửi thông tin";
                    break;
                case 0:
                    lblTrangThai.Text = "Chờ duyệt chính thức";
                    break;
                case 1:
                    lblTrangThai.Text = "Đã duyệt chính thức";
                    break;
                case 2:
                    lblTrangThai.Text = "Không phê duyệt";
                    break;

            }
            txtSoTiepNhan.Text = this.SPDangKy.SoTiepNhan.ToString();
            txtMaHaiQuan.Text = this.SPDangKy.MaHaiQuan;
            txtTenHaiQuan.Text = DonViHaiQuan.GetName(this.SPDangKy.MaHaiQuan);
            if (this.OpenType == OpenFormType.View)
            {
                TopRebar1.Visible = false;
                dgList.AllowDelete = InheritableBoolean.False;
            }
            else
            {
                TopRebar1.Visible = true;
                dgList.AllowDelete = InheritableBoolean.True;
            }
        }

        //-----------------------------------------------------------------------------------------
        /// <summary>
        /// Thêm mới sản phẩm.
        /// </summary>
        private void add()
        {
            SanPhamEditForm f = new SanPhamEditForm();
            f.OpenType = OpenFormType.Insert;
            f.MaHaiQuan = txtMaHaiQuan.Text;
            f.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            f.SPCollection = this.SPDangKy.SPCollection;
            f.spDangKy = SPDangKy;
            f.ShowDialog();

            if (f.SPDetail != null)
            {
                SanPham npl = f.SPDetail;
                npl.Master_ID = this.SPDangKy.ID;
                this.SPDangKy.SPCollection.Add(npl);
                npl.Insert();
                dgList.Refetch();
            }
        }

        #endregion

        //-----------------------------------------------------------------------------------------
        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                e.Row.Cells["DVT_Ten"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Value.ToString());
            }
        }

        //-----------------------------------------------------------------------------------------
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (this.OpenType != OpenFormType.View)
            {
                GridEXSelectedItemCollection items = dgList.SelectedItems;
                foreach (GridEXSelectedItem i in items)
                {
                    if (i.RowType == RowType.Record)
                    {
                        SanPhamEditForm f = new SanPhamEditForm();
                        f.MaHaiQuan = txtMaHaiQuan.Text;
                        f.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        f.SPDetail = (SanPham)e.Row.DataRow;
                        f.SPCollection = this.SPDangKy.SPCollection;
                        f.SPCollection.RemoveAt(i.Position);
                        f.OpenType = OpenFormType.Edit;
                        f.spDangKy = SPDangKy;
                        f.ShowDialog();
                        if (f.SPDetail != null)
                        {
                            this.SPDangKy.SPCollection.Insert(i.Position, f.SPDetail);
                        }
                    }
                    dgList.Refetch();
                    break;
                }
            }
        }

        //-----------------------------------------------------------------------------------------
        private void cmMain_CommandClick(object sender, CommandEventArgs e)
        {
            switch (e.Command.Key)
            {
                case "cmdAdd":
                    this.add();
                    break;
            }
        }

        private void dgList_DeletingRecord(object sender, RowActionCancelEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                if (ShowMessage("Bạn có muốn xóa sản phẩm này không?", true) == "Yes")
                {
                    SanPham npl = (SanPham)e.Row.DataRow;
                    if (npl.ID > 0)
                    {
                        npl.Delete();
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void dgList_DeletingRecords(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (ShowMessage("Bạn có muốn xóa sản phẩm này không?", true) == "Yes")
            {
                GridEXSelectedItemCollection items = dgList.SelectedItems;
                foreach (GridEXSelectedItem i in items)
                {
                    if (i.RowType == RowType.Record)
                    {
                        SanPham npl = (SanPham)i.GetRow().DataRow;
                        if (npl.ID > 0)
                        {
                            npl.Delete();
                        }
                    }
                }
                SPDangKy.TrangThaiXuLy = TrangThaiXuLy.CHO_DUYET;
                SPDangKy.Update();
                lblTrangThai.Text = "Chờ duyệt";
            }
            else
            {
                e.Cancel = true;
            }            
        }
    }
}